package com.googlecode.trabhci2011.stockmarket.client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.googlecode.trabhci2011.stockmarket.shared.StockDataDefs;
import com.googlecode.trabhci2011.stockmarket.shared.StockDataEntry;

public class StockMarket implements EntryPoint, ValueChangeHandler {

	private	Label		dateLabel		= new Label();
	final	DatePicker	dateFrom		= new DatePicker();
	final	DatePicker	dateTo			= new DatePicker();
	final	HashMap<String, String>		data		= new HashMap<String, String>();
	final Button		submit	= new Button("Change");
	final TextBox		name	= new TextBox();
	private Label serverDate = new Label();	
	private HashMap <String, Vector<StockDataEntry>> stockData = new HashMap<String, Vector<StockDataEntry>>();
	private final StockServiceAsync stockSerivce = GWT.create(StockService.class);
	private StockCanvas drawCanvas = null; 
	
	final Vector<CheckBox> v_cbName = new Vector<CheckBox>();
	
	@Override
	public void onModuleLoad() {;

		name.setText("Hello");
		
		Label outLabel	= new Label("outLabel");
		drawCanvas = new StockCanvas(stockData);
		ClickHandler onSubmit = new LabelChangeHandler(outLabel, name);		
		submit.addClickHandler(onSubmit);
		
		RootLayoutPanel rp = RootLayoutPanel.get();
		VerticalPanel vp = new VerticalPanel();
		rp.add(vp);
		
		HorizontalPanel hp = new HorizontalPanel();
		HorizontalPanel hp2 = new HorizontalPanel();
		
		VerticalPanel vp2 = new VerticalPanel();
		
		Iterator<String> it = StockDataDefs.indexNames.keySet().iterator();
		while (it.hasNext()){
			String myString = it.next();
			CheckBox myCB = new CheckBox();
			myCB.setFormValue(myString);
			myCB.setText(StockDataDefs.indexNames.get(myString));
			v_cbName.add(myCB);
			vp2.add(myCB);
		}
		
		vp.add(new HTML("<h1>Hello World</h1>"));
		
		//hp2.add(data);
		dateFrom.addValueChangeHandler(this);
		dateTo.addValueChangeHandler(this);
		name.addValueChangeHandler(this);
		hp2.add(vp2);
		hp2.add(dateFrom);
		hp2.add(dateTo);
		hp2.add(serverDate);
		

		vp.add(hp2);
		
		hp.add(name);
		hp.add(submit);
		
		vp.add(hp);
		vp.add(outLabel);
		vp.add(dateLabel);
		vp.add(drawCanvas.getPanel());
		

	}

	@Override
	public void onValueChange(ValueChangeEvent event) {
		//dateLabel.setText("Date: " + date.getValue());
		StockCallback callback = new StockCallback(stockData, drawCanvas);
		if(dateFrom.getValue() != null)
			data.put(StockDataDefs.START_DATE_NAME, StockDataDefs.DATE_FORMAT.format(dateFrom.getValue()));
		if(dateTo.getValue() != null)
			data.put(StockDataDefs.END_DATE_NAME, StockDataDefs.DATE_FORMAT.format(dateTo.getValue()));
		
		String checkboxString = "";
		String limiter = "";
		for(CheckBox cb : v_cbName){
			if(cb.getValue()){
				checkboxString += limiter + cb.getFormValue();
				limiter = " ";
			}
		}
		data.put(StockDataDefs.INDEX_NAMES, checkboxString);
		stockSerivce.stockServer(data, callback);
	}

}
